//
//  ViewController.m
//  轮播图
//
//  Created by 向祖华 on 16/4/16.
//  Copyright © 2016年 向祖华. All rights reserved.
//

#import "ViewController.h"
#define kScreenW [UIScreen mainScreen].bounds.size.width
#define kScreenH [UIScreen mainScreen].bounds.size.height

static NSInteger pageNumber = 2;//用于记录计时器计时循环
static BOOL leftOrRight = NO;//用于记录手动向左滑动还是向右滑动
static BOOL stopOrBegin = YES;//记录停止和开始按钮的状态

@interface ViewController ()<UIScrollViewDelegate>

@property(nonatomic,strong)UIScrollView*scrolleView;
@property(nonatomic,strong)NSTimer * time;
@property(nonatomic,strong)UIPageControl * pageC;
@property(nonatomic,strong)NSArray * imageNameArr;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //添加停止和开始按钮
    [self addsStopAndBeginController];
    //添加scrollView
    [self setScrollView];
    //添加pageController
    [self setPageController];
    //添加定时器
    self.time = [NSTimer scheduledTimerWithTimeInterval:1.4 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
}
#pragma mark -- 添加scrollView并添加图片
-(void)setScrollView{
    self.scrolleView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, kScreenW, kScreenH-64)];
    self.scrolleView.backgroundColor = [UIColor cyanColor];
    //设置scrollView的尺寸为10个屏幕宽，最后一张和第一张放的是第一张和最后一张图片
    self.scrolleView.contentSize = CGSizeMake(10*kScreenW, 0);
    //初始偏移量
    self.scrolleView.contentOffset = CGPointMake(kScreenW, 0);
    self.scrolleView.bounces = NO;
    self.scrolleView.pagingEnabled = YES;
    self.scrolleView.showsHorizontalScrollIndicator = NO;
    self.scrolleView.delegate =self;
    [self.view addSubview:self.scrolleView];
    
    //设置图片数组
    self.imageNameArr = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8"];
    //循环添加图片
    for (int i = 0; i < 10; i++) {
        UIImageView * imageV = [[UIImageView alloc] initWithFrame:CGRectMake(i*kScreenW, 0, kScreenW, self.scrolleView.frame.size.height)];
        if (i == 0) {
            imageV.image = [UIImage imageNamed:self.imageNameArr[7]];
        }
        else if (i == 9) {
            imageV.image = [UIImage imageNamed:self.imageNameArr[0]];
        }else{
            imageV.image = [UIImage imageNamed:self.imageNameArr[i-1]];
        }
        
        [self.scrolleView addSubview:imageV];
    }

}
#pragma mark -- 添加pageController
-(void)setPageController{
    self.pageC = [[UIPageControl alloc] initWithFrame:CGRectMake(50, kScreenH-80, kScreenW - 100, 30)];
    self.pageC.numberOfPages =self.imageNameArr.count;
    [self.view addSubview:self.pageC];
    self.pageC.pageIndicatorTintColor = [UIColor brownColor];
    self.pageC.currentPageIndicatorTintColor = [UIColor redColor];
    self.pageC.currentPage = 0;//初始位置为0
}
#pragma mark -- scrollViewDeletegate
//scrollView滑动
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //当scrollView滑动时，设置page
    CGFloat scroll = scrollView.contentOffset.x/kScreenW;
    NSInteger number = (NSInteger)scroll;//偏移了几个屏幕宽的距离
    //如果小数部分大于半屏
    if (scroll - number>kScreenW/2 && number < 9&& number > 0) {
        self.pageC.currentPage = number;
    }
    if (scroll - number<=kScreenW/2) {
        self.pageC.currentPage = number-1;
    }
    //最后一张和第一张对应的page
    if (number == 9) self.pageC.currentPage = 0;
    if (number == 0) self.pageC.currentPage = 7;
}
//scrollView结束减速
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    //当滑到第十张时，设置偏移量为0
    if (self.scrolleView.contentOffset.x >= 9*kScreenW) {
        self.scrolleView.contentOffset = CGPointMake(kScreenW, 0);
        leftOrRight = YES;
    }
    
    //当滑到第一张的时，设置偏移量为8
    if (self.scrolleView.contentOffset.x <=0*kScreenW && leftOrRight == NO) {
        self.scrolleView.contentOffset = CGPointMake(8*kScreenW, 0);
    }
    leftOrRight = NO;
}
//开始拖拽
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    //结束计时
    [self.time invalidate];
    self.time = nil;
}
//结束拖拽
-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    //开始计时
    if (stopOrBegin == YES)[self beginAction];
    //拖拽结束后给记录轮播到第几张的变量赋值
    NSInteger number = (NSInteger)self.scrolleView.contentOffset.x/kScreenW;
    if (number==0) pageNumber = 2;
    if (number == 9) pageNumber = 2;
    if (number > 0 && number < 9) {
        pageNumber = number + 1;
    }
}
#pragma mark -- 添加停止和开始按钮
-(void)addsStopAndBeginController{
    UIBarButtonItem * leftItem = [[UIBarButtonItem alloc] initWithTitle:@"开始" style:(UIBarButtonItemStylePlain) target:self action:@selector(beginAction)];
    self.navigationItem.leftBarButtonItem = leftItem;
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc] initWithTitle:@"停止" style:(UIBarButtonItemStylePlain) target:self action:@selector(stopAction)];
    self.navigationItem.rightBarButtonItem =rightItem;
}

-(void)beginAction{
    stopOrBegin = YES;
    //如果计时器已开启  先停止
    if (self.time) [self stopAction];
    self.time = [NSTimer scheduledTimerWithTimeInterval:1.4 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
}

-(void)stopAction{
    stopOrBegin = NO;
    [self.time invalidate];
    self.time = nil;
}
//定时器相应事件
-(void)timeAction{
    if (pageNumber == 10) {
        pageNumber = 2;
        self.scrolleView.contentOffset = CGPointMake(kScreenW,0);
        [UIView animateWithDuration:0.5 animations:^{
            self.scrolleView.contentOffset = CGPointMake(pageNumber*kScreenW,0);
        }];
    }else{
        [UIView animateWithDuration:0.5 animations:^{
            self.scrolleView.contentOffset = CGPointMake((pageNumber)*kScreenW,0);
        }];
    }
    pageNumber++;
}

#pragma mark -- 懒加载
-(NSArray *)imageNameArr{
    if (!_imageNameArr) {
        _imageNameArr = [[NSArray alloc] init];
    }
    return _imageNameArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
